﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace CustomerService
{
    public partial class OrderTab : System.Web.UI.UserControl
    {
        private SalesManagerEntities _context;
        private tblCustomer _customer;
        private IOrderedEnumerable<tblOrderHeader> _orderList;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public new void Load(SalesManagerEntities context, tblCustomer customer)
        {
            _context = context;
            _customer = customer;
            TabContainer1.Enabled = customer.cachedOrderHeaders.Count() > 0;
            
            _orderList = customer.cachedOrderHeaders.OrderByDescending(oh=>oh.OrderDate);
            if (_orderList.Count() > 0 && lvOrderList.SelectedIndex < 0)
                lvOrderList.SelectedIndex = 0;
            if (!IsPostBack)
            {
                lvOrderList.DataSource = _orderList;
                lvOrderList.DataBind();
            }
            SetDetail(false);
        }

        protected void OrderListSelectedChanged(object sender, EventArgs e)
        {
        }

        protected void OrderListSelectedIndexChanging(object sender, ListViewSelectEventArgs e)
        {
            lvOrderList.SelectedIndex = e.NewSelectedIndex;
            lvOrderList.DataSource = _orderList;
            lvOrderList.DataBind();
            SetDetail(true);
        }

        private void SetDetail( bool forceUpdate )
        {
            if (lvOrderList.SelectedIndex >= 0)
            {
                ((OrderDetailTab)TabContainer1.FindControl("TabPanelDetails").FindControl("tabOrderDetail"))
                    .Load(_context, _orderList.ElementAt(lvOrderList.SelectedIndex), forceUpdate);
                ((tabShipTo)TabContainer1.FindControl("TabPanelShipping").FindControl("tabShipTo"))
                    .Load(_context, _orderList.ElementAt(lvOrderList.SelectedIndex), forceUpdate);
                ((tabRefundsReturns)TabContainer1.FindControl("tabPanelRefunds").FindControl("tabRefundsReturns"))
                    .Load(_context, _orderList.ElementAt(lvOrderList.SelectedIndex), forceUpdate);
                ((TransactionsTab)TabContainer1.FindControl("tabPanelTransactions").FindControl("tabTransactions"))
                    .Load(_context, _orderList.ElementAt(lvOrderList.SelectedIndex).Transactions);
                ((tabTracking)TabContainer1.FindControl("TabPanelTracking").FindControl("tabTracking"))
                    .Load(_context, _orderList.ElementAt(lvOrderList.SelectedIndex), forceUpdate);
            }
        }

        protected void btnAddNewOrder_Click(object sender, EventArgs e)
        {
            tblOrderHeader newOrder = new tblOrderHeader();
            newOrder.Customer = _customer;
            newOrder.ShipTo_Person = _customer.Person;
            newOrder.ShipTo_Address = _customer.Person.Address;
            newOrder.OwnerID = (int)Session["OwnerID"];

            newOrder.OrderType = (from c in _context.codeOrderType where c.OrderTypeCode == "S" select c).First();
            newOrder.OrderStatus = (from c in _context.codeOrderStatus where c.OrderStatusCode == "P" select c).First();
            string orderSource = ConfigurationManager.AppSettings["OrderSource"];
            newOrder.OrderSource = (from os in _context.codeOrderSource where os.Description == orderSource select os).First();

            _context.AddTotblOrderHeader(newOrder);
            _context.SaveChanges();

            _orderList = _customer.cachedOrderHeaders.OrderByDescending(oh => oh.OrderDate);
            lvOrderList.DataSource = _orderList;
            lvOrderList.SelectedIndex = 0;
            lvOrderList.DataBind();
        }

        protected void OrderListSelectedIndexChanged(object sender, EventArgs e)
        {
        }
    }
}